package cn.xyz.mianshi.service.impl;

import java.util.List;

import org.bson.BasicBSONObject;
import org.bson.types.ObjectId;
import org.mongodb.morphia.query.Query;
import org.mongodb.morphia.query.UpdateOperations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

import cn.xyz.commons.support.mongo.MongoOperator;
import cn.xyz.commons.utils.DateUtil;
import cn.xyz.commons.utils.StringUtil;
import cn.xyz.mianshi.utils.KSessionUtil;
import cn.xyz.mianshi.vo.LiveRoom;
import cn.xyz.mianshi.vo.LiveRoom.LiveRoomMember;

@Service
public class LiveRoomManagerImpl extends MongoRepository<LiveRoom, ObjectId> {

	@Autowired
	private UserManagerImpl userManager;
	
	//创建直播间
	public LiveRoom createLiveRoom(LiveRoom room){
		try {
			room.setNickName(userManager.getUser(room.getUserId()).getNickname());
			room.setCreateTime(DateUtil.currentTimeSeconds());
			room.setNumbers(1);
			room.setUrl(room.getUserId()+"_"+DateUtil.currentTimeSeconds());
			ObjectId id=(ObjectId) save(room).getId();
			
			LiveRoomMember member=new LiveRoomMember();
			member.setUserId(room.getUserId());
			member.setRoomId(id);
			member.setCreateTime(DateUtil.currentTimeSeconds());
			member.setNickName(userManager.getUser(room.getUserId()).getNickname());
			saveEntity(member);
			
			room.setUrl(KSessionUtil.getConfig().getLiveUrl()+room.getUrl());
			return room;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	//修改直播间信息
	public void updateLiveRoom(LiveRoom room){
		UpdateOperations<LiveRoom> ops=createUpdateOperations();
		if(!StringUtil.isEmpty(room.getName()))
			ops.set("name", room.getName());
		if(!StringUtil.isEmpty(room.getUrl()))
			ops.set("url", room.getUrl());
		if(!StringUtil.isEmpty(room.getNotice()))
			ops.set("notice", room.getNotice());
		ops.disableValidation();
		updateAttributeByOps(room.getRoomId(), ops);
		
	}
	
	//删除直播间
	public void deleteLiveRoom(ObjectId roomId){
		deleteById(roomId);
		//删除直播间中的成员
		Query<LiveRoomMember> query=dsForRW.createQuery(LiveRoomMember.class);
		query.filter("roomId", roomId);
		dsForRW.delete(query);
	}
	
	public List<LiveRoom> findLiveRoomList(String name,String nickName,Integer userId,int pageIndex, int pageSize){
		Query<LiveRoom> query=createQuery();
		if(!StringUtil.isEmpty(name))
			query.filter("name",name);
		if(!StringUtil.isEmpty(nickName))
			query.filter("nickName",nickName);
		if(0!=userId)
			query.filter("userId",userId);
		query.order("-createTime");
		query.offset(pageIndex * pageSize);
		
		List<LiveRoom> roomList=query.limit(pageSize).asList();
		for (LiveRoom liveRoom : roomList) {
			if(!liveRoom.getUrl().contains("//"))
					liveRoom.setUrl(KSessionUtil.getConfig().getLiveUrl()+liveRoom.getUrl());
		}
		return roomList;
	}
	
	//加入直播间
	public void enterIntoLiveRoom(Integer userId,ObjectId roomId){
		Query<LiveRoomMember> q=dsForRW.createQuery(LiveRoomMember.class);
		q.filter("roomId",roomId);
		q.filter("userId",userId);
		//查询该直播间 是否已存在 改用户
		if(null!=q.get())
			return;
		LiveRoomMember member=new LiveRoomMember();
		member.setUserId(userId);
		member.setRoomId(roomId);
		member.setCreateTime(DateUtil.currentTimeSeconds());
		member.setNickName(userManager.getUser(userId).getNickname());
		saveEntity(member);
		
		//修改直播间总人数
		/*DBObject value=new BasicDBObject(MongoOperator.INC, 1);
		updateAttributeByIdAndKey(roomId, "numbers", value);*/
		
		
		UpdateOperations<LiveRoom> ops=createUpdateOperations();
		ops.inc("numbers", 1);
		updateAttributeByOps(roomId, ops);
	}
	//退出直播间
	public void exitLiveRoom(Integer userId,ObjectId roomId){
		//删除直播间成员
		Query<LiveRoomMember> q=dsForRW.createQuery(LiveRoomMember.class);
		q.filter("roomId",roomId);
		q.filter("userId",userId);
		dsForRW.delete(q);
		
		//修改直播间总人数
		UpdateOperations<LiveRoom> ops=createUpdateOperations();
		ops.inc("numbers", -1);
		updateAttributeByOps(roomId, ops);
	}
	
	public List<LiveRoomMember> findLiveRoomMemberList(ObjectId roomId,String name,String nickName,Integer userId,int pageIndex, int pageSize){
		Query<LiveRoomMember> query=dsForRW.createQuery(LiveRoomMember.class);
		/*if(!StringUtil.isEmpty(name))
			query.filter("name",name);*/
		if(null!=roomId)
			query.filter("roomId",roomId);
		if(!StringUtil.isEmpty(nickName))
			query.filter("nickName",nickName);
		if(0!=userId)
			query.filter("userId",userId);
		query.offset(pageIndex * pageSize);
		
		return query.limit(pageSize).asList();
	}
	
}
